﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace JustTank
{
    public class TankBot : Tank
    {
        public new const string CollisionGroupString = "tankBot";
        public ConsoleColor Color = ConsoleColor.DarkRed;
        
        private int randomDirection;
        private bool shoot;
        public bool ThereIsCollision { get; set;}

        public TankBot(MatrixCoords topLeft)
            : base(topLeft)
        {
            this.shoot = false;
            
        }

        protected override void UpdatePosition()
        {
            Random randomGenerator = RandomProvider.GetThreadRandom();
            this.randomDirection = randomGenerator.Next(1, 500);

            if (this.randomDirection >= 400 && this.randomDirection < 500)
            {
                ChangeDirection( GetRandomTurn() );
            }

            this.TopLeft += this.Speed;
        }

        private int GetRandomTurn()
        {
            if (randomDirection % 2 == 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        public void ChangeDirection(int direction)
        {
            if (this.Speed.Col == 0)
            {
                this.Speed.Row = 0;
                this.Speed.Col = direction;
                if (this.Speed.Col < 0)
                {
                    this.body = GetTankBody(TankDirection.Left);
                }
                else
                {
                    this.body = GetTankBody(TankDirection.Right);
                }

               // this.TopLeft += this.Speed;
            }
            else
            {
                this.Speed.Col = 0;
                this.Speed.Row = direction;
                if (this.Speed.Row < 0)
                {
                    this.body = GetTankBody(TankDirection.Up);
                }
                else
                {
                    this.body = GetTankBody(TankDirection.Down);
                }

              //  this.TopLeft += this.Speed;
            }
        }

        public override void Update()
        {            
            this.UpdatePosition();
            this.ShootBullet();
        }        

        private void ShootBullet()
        {
            Random randomGenerator = RandomProvider.GetThreadRandom();
            int randomNumber = randomGenerator.Next(1, 20);

            if (randomNumber  < 4)
            {
                this.shoot = true;
            }
        }

        public override IEnumerable<GameObject> ProduceObjects()
        {
            List<BotBullet> bullets = new List<BotBullet>();
            if (this.shoot == true)
            {
                bullets.Add(new BotBullet(new MatrixCoords(this.topLeft.Row, this.topLeft.Col), this.GetTankDirection(this.Speed)));

                this.shoot = false;
            }

            return bullets;
        }

    }
}
